package com.example.zzm.yuejian03.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Activity.TrainActivity;
import com.example.zzm.yuejian03.Activity.TraindetailsActivity;
import com.example.zzm.yuejian03.Bean.TrainBean;
import com.example.zzm.yuejian03.R;
import com.example.zzm.yuejian03.Server;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.ViewHolder> {

    private List<TrainBean> List;
    private Context Context;
    private LayoutInflater LayoutInflater;

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView,textView2,textView3;

        public ViewHolder(View view){
            super(view);
            imageView= (ImageView)view.findViewById(R.id.imageView);
            textView = (TextView)view.findViewById(R.id.textView);
            textView3 = (TextView)view.findViewById(R.id.textView3);
            textView2 = (TextView)view.findViewById(R.id.textView2);
        }
    }


    public TrainAdapter(Context Context, List<TrainBean> List){
        this.Context = Context;
        this.List = List;
        LayoutInflater = LayoutInflater.from(Context);
    }

    //自定义 设置数据list
    public void setList( List<TrainBean> list) {
        this.List = list;
        notifyDataSetChanged();//通知RV刷新数据
    }

    @Override
    public TrainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(TrainAdapter.ViewHolder holder, int position) {

        final TrainBean train = List.get(position);

        Picasso.with(Context)
                .load(Server.IMAGEURL+train.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.imageView);
        holder.textView.setText(train.getTrain_name());
        holder.textView2.setText((train.getIntro()));
        holder.textView3.setText((train.getTime()));

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //取出当前item的id
                int train_id = train.getid();
                Toast.makeText(Context, "" + train_id, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(Context, TraindetailsActivity.class);
                intent.putExtra("train_id",train.getid());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                Context.startActivity(intent);

            }
        });
}
    @Override
    public int getItemCount() {
        return List.size();
    }
}
